import java.util.Scanner;
// synchronized 也能保证内存可见性
public class ThreadDemo13 {
    public static int flg = 0;

    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(() -> {
            while (true) {
                synchronized (locker) {
                    if (flg != 0) {
                        break;
                    }
                }
            }
            System.out.println("t线程结束");
        });

        Thread t2 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个整数：");
            flg = scanner.nextInt();
        });

        t1.start();
        t2.start();
    }
}
